import 'package:dio/dio.dart';
import 'package:get/get.dart';

import '../../../bean/bean_train.dart';
import '../../../net/dio_utils.dart';

class TeacherTrainCourseController extends GetxController {


  List<TeacherTrainBean> _list;
  bool _isLoading = true;
  int _pageIndex = 1;

  bool getIsLoading() {
    return _isLoading;
  }

  List<TeacherTrainBean> getList() {
    return _list;
  }

  CancelToken _signCancelToken;
  int _totalPage = 0;

  Future<bool> requestListFunction({bool click = false}) async {
    _isLoading = true;
    if (click) {
      update(["root"]);
    }

    if (_signCancelToken != null && !_signCancelToken.isCancelled) {
      _signCancelToken.cancel();
    }
    _signCancelToken = new CancelToken();
    List<TeacherTrainBean> courseList = [];
    //答题数据
    ResponseInfo statisRresponseInfo = await DioUtils.instance.postRequest(
        url: HttpHelper.trainTeacherCourseList,
        cancelTag: _signCancelToken,
        jsonMap: {
          "pageIndex": _pageIndex,
        });
    if (statisRresponseInfo.success) {
      Map data = statisRresponseInfo.data;
      _totalPage = data["totalPage"] ?? 0;
      List list = data["list"];
      if (list != null) {
        list.forEach((element) {
          courseList.add(TeacherTrainBean.fromMap(element));
        });
      }
    }

    if (_pageIndex == 1) {
      _list = courseList;
    } else {
      if (courseList.isNotEmpty) {
        _list.addAll(courseList);
      }
    }

    _isLoading = false;
    update(["root"]);
    return true;
  }
}
